/**
 * 用户详情对话框（可用于添加和修改对话框）
 */
var UserDlg = {
    userData: {},
    deptTree: null,
    validateFields: {
        account: {
            validators: {
                notEmpty: {
                    message: '账号不能为空'
                },
                stringLength: {
                    max: 32,
                    message: '请输入32位以内的账号'
                },
                regexp: {
                    regexp: /^[a-z][a-z\_]*[a-z]$/,
                    message: '账号只能只能包含小写和下划线，以字母开头和结尾'
                }
            }
        },
        sex: {
            validators: {
                notEmpty: {
                    message: '性别不能为空'
                }
            }
        },
        deptName: {
            validators: {
                notEmpty: {
                    message: '部门不能为空'
                }
            }
        },
        name: {
            validators: {
                notEmpty: {
                    message: '姓名不能为空'
                },
                regexp: {
                    regexp: /^([\u4e00-\u9fa5]{2,10})$/,
                    message: '请输入2-10位的中文姓名'
                }
            }
        },
        birthday: {
            validators: {
                notEmpty: {
                    message: '出生日期不能为空'
                },
                date: {
                    format: 'YYYY/MM/DD',
                    message: '出生日期格式错误'
                }
            }
        },
        phoneNum: {
            validators: {
                notEmpty: {
                    message: '电话不能为空'
                },
                regexp: {
                    regexp: /^1[3|4|5|8][0-9]\d{8}$/,
                    message: '请输入正确的电话'
                }
            }
        },
        post: {
            validators: {
                notEmpty: {
                    message: '职务不能为空'
                }
            }
        },
    }
};

/**
 * 清除数据
 */
UserDlg.clearData = function () {
    this.userData = {};
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
UserDlg.set = function (key, val) {
    this.userData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};

/**
 * 获取对话框中的数据
 *
 * @param key 数据的名称
 */
UserDlg.get = function (key) {
    return $("#" + key).val();
};

/**
 * 关闭此对话框
 */
UserDlg.close = function () {
    parent.layer.close(window.parent.User.layerIndex);
};

/**
 * 收集数据
 */
UserDlg.collectData = function () {
    this.set('id').set('account').set('sex').set('deptId').set('name').set('birthday').set('phoneNum').set('post');
};

/**
 * 验证数据是否为空
 */
UserDlg.validate = function () {
    var bootstrapValidator = $("#userForm").data('bootstrapValidator');
    bootstrapValidator.resetForm();
    bootstrapValidator.validate();
    return bootstrapValidator.isValid();
};

/**
 * 提交添加用户
 */
UserDlg.addUserSubmit = function (url) {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Common.ctxPath + "/user/add", function (result) {
        if (result.success) {
            Common.success(result.message + "!");
            window.parent.User.table.refresh();
            UserDlg.close();
        } else {
            Common.error(result.message + "!");
        }
    }, function (result) {
        Common.error(result.responseJSON.message + "!");
    });
    ajax.set(this.userData);
    ajax.start();
};

/**
 * 提交修改
 */
UserDlg.editUserSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Common.ctxPath + "/user/update", function (result) {
        if (result.success) {
            Common.success(result.message + "!");
            window.parent.User.table.refresh();
            UserDlg.close();
        } else {
            Common.error(result.message + "!");
        }
    }, function (result) {
        Common.error(result.responseJSON.message + "!");
    });
    ajax.set(this.userData);
    ajax.start();
};

/**
 * 点击部门input框时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
UserDlg.onClickDept = function (e, treeId, treeNode) {
    $("#deptName").val(UserDlg.deptTree.getSelectedVal());
    $("#deptId").val(treeNode.id);
};

/**
 * 双击部门input框
 */
UserDlg.onDblClickDept = function (e, treeId, treeNode) {
    $("#deptName").val(UserDlg.deptTree.getSelectedVal());
    $("#deptId").val(treeNode.id);
    $("#deptContent").fadeOut("fast");
};

/**
 * 显示部门选择的树
 *
 * @returns
 */
UserDlg.showDeptSelectTree = function () {
    Common.showInputTree("deptName", "deptContent");
};

$(function () {
    Common.initValidator("userForm", UserDlg.validateFields);
    var deptTree = new $ZTree("deptTree", "/tree/dept");
    deptTree.bindOnClick(UserDlg.onClickDept);
    deptTree.bindOnDblClick(UserDlg.onDblClickDept)
    deptTree.init();
    UserDlg.deptTree = deptTree;
});
